import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class Controller {

	private Table table;
	private SimulationUI simulation;
	
	public Controller(){
		table = new Table();
		simulation = new SimulationUI();
		simulation.setButtonListener(new btnGenerateListener());
		simulation.setExtendedState(Frame.MAXIMIZED_BOTH);
		simulation.setVisible(true);
		simulation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	class btnGenerateListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			int populationSize = simulation.getTxtPopulationSize();
			int sampleSize = simulation.getTxtSampleSize();
			int lowerBound = simulation.getTxtLower();
			int upperBound = simulation.getTxtUpper();
			
			if(simulation.getDistribution().equalsIgnoreCase("Uniform"))
				table.generateUniformDistribution(populationSize, lowerBound, upperBound);
			else if(simulation.getDistribution().equalsIgnoreCase("Random"))
				table.generateRandomDistribution(populationSize, lowerBound, upperBound);
			else if(simulation.getDistribution().equalsIgnoreCase("Normal"))
				table.generateNormalDistribution(populationSize, lowerBound, upperBound);
			else if(simulation.getDistribution().equalsIgnoreCase("Skewed"))
				table.generateSkewedDistribution(populationSize, lowerBound, upperBound);
			else if(simulation.getDistribution().equalsIgnoreCase("Bimodal"))
				table.generateBimodalDistribution(populationSize, lowerBound, upperBound);
			// Population
			simulation.setPopulationMean(table.getMean());
			simulation.setPopulationVariance(table.getVariance());
			simulation.setPopulationTableModel(table.generateModel());
			simulation.setPopulationChart(table.generateChart());
			simulation.setPopulationSliderValues(0, populationSize + 200);
			simulation.setPopulationSliderListener(new sliderPopulationListener());
			simulation.setCurrentPopulationValue(populationSize);
			
			//Sample
			table.generateSamplingDistribution(sampleSize);
			simulation.setSampleTableModel(table.generateSampleModel());
			simulation.setSampleChart(table.generateSamplingChart());
		}
		
	}
	
	class sliderPopulationListener implements ChangeListener {

		@Override
		public void stateChanged(ChangeEvent arg0) {
			// TODO Auto-generated method stub
			int lowerBound = simulation.getTxtLower();
			int upperBound = simulation.getTxtUpper();
			int current = simulation.getPopulationSliderValue() + 50;
			
			if(simulation.getDistribution().equalsIgnoreCase("Uniform"))
				table.generateUniformDistribution(current, lowerBound, upperBound);
			else if(simulation.getDistribution().equalsIgnoreCase("Random"))
				table.generateRandomDistribution(current, lowerBound, upperBound);
			else if(simulation.getDistribution().equalsIgnoreCase("Normal"))
				table.generateNormalDistribution(current, lowerBound, upperBound);
			else if(simulation.getDistribution().equalsIgnoreCase("Skewed"))
				table.generateSkewedDistribution(current, lowerBound, upperBound);
			else if(simulation.getDistribution().equalsIgnoreCase("Bimodal"))
				table.generateBimodalDistribution(current, lowerBound, upperBound);
	
			// Population
			simulation.setPopulationMean(table.getMean());
			simulation.setPopulationVariance(table.getVariance());
			simulation.setPopulationTableModel(table.generateModel());
			simulation.setPopulationChart(table.generateChart());
			simulation.setCurrentPopulationValue(current);
			// Sample
			table.generateSamplingDistribution(simulation.getTxtSampleSize());
			simulation.setSampleTableModel(table.generateModel());
		}
		
	}

}
